package view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import algorithm.Priority;
import algorithm.RoundRobin;
import enumMsg.ProState;
import pojo.ProcessPojo;
import utils.TimeUtils;

import java.awt.Font;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import java.awt.Color;

public class Priview {

	private JFrame frame;
	private Priority pp;//引入优先级算法
	private Vector data;
	private Vector Title;
	private DefaultTableModel model;
	private JTextField pcbsum;
	private JTable table;
	private JButton button;
	private JButton button_1;
	private JButton button_1_1;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Priview window = new Priview();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Priview() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("基于优先级模拟");
		frame.setVisible(true);
		frame.setBounds(100, 100, 934, 530);
		frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("优先级模拟算法");
		label.setFont(new Font("宋体", Font.PLAIN, 20));
		label.setBounds(364, 0, 191, 60);
		frame.getContentPane().add(label);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(0, 107, 916, 376);
		frame.getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setRowHeight(18);
		table.setForeground(Color.WHITE);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		table.setEnabled(false);
		table.setBackground(Color.DARK_GRAY);
		scrollPane.setViewportView(table);
		
		button = new JButton("初始化就绪队列");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				initQueue();
			}

			
		});
		button.setBounds(345, 60, 151, 33);
		frame.getContentPane().add(button);
		
		 button_1 = new JButton("开始模拟");
		 button_1.setEnabled(false);
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				start();
			}

			
		});
		button_1.setBounds(543, 60, 151, 33);
		frame.getContentPane().add(button_1);
		
		button_1_1 = new JButton("生成抢占进程");
		button_1_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				addPcb();
			}

			
		});
		button_1_1.setEnabled(false);
		button_1_1.setBounds(726, 60, 151, 33);
		frame.getContentPane().add(button_1_1);
		
		JTextArea textArea_1 = new JTextArea();
		textArea_1.setText("进程数");
		textArea_1.setBounds(127, 66, 50, 24);
		frame.getContentPane().add(textArea_1);
		
		pcbsum = new JTextField();
		pcbsum.setColumns(10);
		pcbsum.setBounds(205, 66, 86, 24);
		frame.getContentPane().add(pcbsum);
		
	
	}
	private void addPcb() {
		ProcessPojo pcb=new ProcessPojo();
		pcb.setName("newpcb");
		pcb.setState(ProState.READY.getState());//默认为就绪状态
		pcb.setCpuTime(0);//初始化cputime为0
		pcb.setNeedTime(3);//随机数设置所需时间
		pcb.setOrder(0);//设置0最高优先级
		TimeUtils timeUtils=new TimeUtils(true);//设置模拟程序
		pcb.setTimeUtils(timeUtils);
		this.pp.addProcess(pcb);
		System.out.println("添加成功");
	}
	private void start() {
		new Thread(()->{
			while(pp.getSize()>0) {
				pp.start();
				ArrayList<ProcessPojo>runList=pp.getRunList();
				for(ProcessPojo pcb:runList) {
					Vector rows=new Vector<>();
					rows.add(pcb.getName());rows.add(pcb.getState());rows.add(pcb.getNeedTime());rows.add(pcb.getCpuTime());rows.add(pcb.getOrder());
					data.add(rows);
					model.setDataVector(data, Title);
					model.fireTableDataChanged();
					table=new JTable(model);
					table.validate();
					table.updateUI();
				}
			}	
			System.out.println("模拟完毕.");
		}).start(); 
		
		
	}
	private void initQueue() {
		//初始化表格标题
		this.Title=new Vector<>();
		Title.add("进程名");
		Title.add("当前状态");
		Title.add("需要时间");
		Title.add("cpu占用时间");
		Title.add("优先级");	
		//初始化就绪队列的数据
		this.pp=new Priority();

		String strp=pcbsum.getText();
		if(strp.length()==0) {
			//输入格式错误
			new WarnView();
		}
		else {
			int pcbcnt=Integer.parseInt(strp);//获取输入框的值
			
			this.pp.init(pcbcnt);//设置进程数为5
			LinkedList<ProcessPojo>queue=this.pp.getReadyQueue();
			this.data=new Vector<>();
			for(ProcessPojo pcb:queue) {
				Vector rows=new Vector<>();
				rows.add("初始:"+pcb.getName());rows.add("初始:"+pcb.getState());rows.add("初始:"+pcb.getNeedTime());rows.add("初始:"+pcb.getCpuTime());rows.add("初始:"+pcb.getOrder());
				data.add(rows);
			}
			model = new DefaultTableModel(data,Title);
			table.setModel(model);
			button_1.setEnabled(true);
			button_1_1.setEnabled(true);
			System.out.println("就绪队列初始化成功");
	}
	}
}
